<template>
  <div class="app-container">
    <!-- 高级查询 -->
    <el-form v-show="searchVisible" ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item label="合同编码:">
        <el-input v-model="queryParams.contractCode" size="small" placeholder="请输入合同编码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="合同名称:">
        <el-input v-model="queryParams.contractName" size="small" placeholder="请输入合同名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="isMultiple" @click="handleLogicalDelete">作废</el-button>
      </el-col>
      <right-toolbar :show-search.sync="searchVisible" @queryTable="handleQuery" />
    </el-row>

    <!-- 数据列表 -->
    <el-table v-loading="loading" :data="page.records" :cell-style="cellStyle" highlight-current-row @row-click="handleRowClick" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="合同编号" align="center" show-overflow-tooltip width="188" prop="contractCode" >
        <template slot-scope="scope">
          {{ scope.row.contractCode }}
        </template>
      </el-table-column>
      <el-table-column label="合同名称" width="165" align="center" show-overflow-tooltip prop="contractName" />
      <el-table-column label="签约日期" width="165" align="center" prop="signingDate" />
      <el-table-column label="合同开始时间" width="165" align="center" show-overflow-tooltip prop="startDate" />
      <el-table-column label="合同结束时间" width="165" align="center" prop="endDate" />
      <el-table-column label="签约人" width="125" align="center" show-overflow-tooltip prop="signatory" />
      <el-table-column label="合同剩余时间" width="165" align="center" prop="surplusDate" />

      <el-table-column label="操作" align="center" fixed="left" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button  size="mini" type="text" icon="el-icon-edit" @click="handleEdit(scope.row)">修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination v-show="page.total>0" :total="page.total" :page.sync="page.current" :limit.sync="page.pageSize" @pagination="handleQuery" />
    <contract-detail v-if="rowId && detailVisible" :id="rowId" class="detail-view" @close="handleDetailClose" @callback="handleQuery" />
    <contract-save v-if="editVisible" :id="detail.id" @callback="handleQuery" @close="editVisible = false" style="background-color: rgb(245, 246, 249,0.5);"/>
  </div>
</template>

<script>
import ListMixin from '@/mixins/List'
import CustomerApi from '@/api/basic/CustomerApi'
import ContractDetail from './ContractDetail.vue'
import ContractSave from './ContractSave.vue'

export default {
  name: 'ContractList',
  components: {
    ContractDetail,
    ContractSave
  },
  mixins: [ListMixin],
  data() {
    return {
      options: {
        customerTypeOptions: [],
        sysYnTypeOptions: this.$constant.sysYnTypeOptions
      },
      queryParams: {
        contractCode: null,
        contractName: null,
        isValid: this.$constant.Y
      },
      page: {
        current: 1,
        pageSize: 10,
        total: 0,
        records: []
      },
      loading: true,
      searchVisible: true,
      multipleSelection: [],
      isSingle: true,
      isMultiple: true
    }
  },
  created() {
    this.handleQuery()
    this.loadOptions()
  },
  methods: {
    loadOptions() {
    },
    handleAdd() {
      this.detail = {}
      this.editVisible = true
    },
    handleView(row) {
      this.$router.push({ path: '/contract/view/' + row.id })
    },
    handleLogicalDelete(row) {
      const ids = row.id || this.multipleSelection.map(item => item.id)
      this.$confirm('是否确认作废数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {

      }).then(() => {
        this.handleQuery()
        this.$message.success('作废成功')
      })
    },
    handleEdit(row) {
      this.detail = row
      this.editVisible = true
    },
    handleQuery() {
      this.loading = true

       this.page.records =[{
        "id":"1",
        "contractCode":"HTBM001",
        "contractName":"物流服务合同",
        "signingDate":"2023-03-03",
        "startDate":"2023-03-03",
        "endDate":"2024-03-03",
        "signatory":"张三",
        "surplusDate":"365天",
      },{
        "id":"2",
        "contractCode":"HTBM002",
        "contractName":"汽车零部件储运合同",
        "signingDate":"2023-03-03",
        "startDate":"2023-03-03",
        "endDate":"2024-03-03",
        "signatory":"李四",
        "surplusDate":"365天",
      },{
        "id":"3",
        "contractCode":"HTBM003",
        "contractName":"长途运输合同",
        "signingDate":"2023-03-03",
        "startDate":"2023-03-03",
        "endDate":"2024-03-03",
        "signatory":"王五",
        "surplusDate":"365天",
      }]
      this.page.total = 5
      this.loading = false
    },
    handleExport() {
      const that = this
      this.$confirm('是否确认导出数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return CustomerApi.exportData(that.queryParams)
      }).then(response => {
        this.$common.download(response.data)
      })
    },
    handleSelectionChange(selection) {
      this.multipleSelection = selection
      this.isSingle = selection.length !== 1
      this.isMultiple = !selection.length
    },
    handleRowClick(row, column, event) {
      console.log("1")
      if (column.type === 'selection') {
        return
      }
      if (column.property === 'contractCode') {
        this.rowId = row.id
        this.detailVisible = true
      } else {
        this.detailVisible = false
      }
    },
    cellStyle({ row, column, rowIndex, columnIndex }) {
      if (column.property === 'contractCode') {
        return {
          color: '#3E84E9',
          cursor: 'pointer'
        }
      } else {
        return ''
      }
    },
    resetQuery() {
      this.page.current = 1
      this.queryParams = {
        contractCode: null,
        contractName: null,
        isValid: this.$constant.Y
      }
      this.handleQuery()
    }
  }
}
</script>
